package com.mantru.iotpot.Adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.mantru.iotpot.Adapter.Base.AppAdapter
import com.mantru.iotpot.Adapter.Base.AppHolder
import com.mantru.iotpot.Bean.DishesCreateBean
import com.mantru.iotpot.Bean.DishesTimePageItemBean
import com.mantru.iotpot.R

class DishesTimePageAdapter (var context: Context, layoutResId: Int, var data: MutableList<DishesCreateBean>): AppAdapter<DishesCreateBean>(context, layoutResId, data) {

    var dishesTimePageItemList: MutableList<DishesTimePageItemBean> = mutableListOf()
    var dishesTimePageItemBeans: MutableList<DishesTimePageItemBean> = mutableListOf()
    var dishesTimePageItemAdapter: DishesTimePageItemAdapter? = null

    override fun convert(holder: AppHolder?, position: Int, item: DishesCreateBean) {

        var recyclerView: RecyclerView? = null

        recyclerView = holder?.getView<RecyclerView>(R.id.adapter_time_show_content_recyclerview)
        holder?.setVisibility(R.id.adapter_time_show_line1, position != 0)
            ?.setText(R.id.adapter_time_show_time, item.time_value)
            ?.setText(R.id.adapter_time_show_content_step, getStepName(position, item.steep_type))

        //初始化recyclerview
        setRecyclerView(recyclerView!!)
        putData(item)
    }

    //获取步骤名称
    private fun getStepName(position: Int, type: String): String{
        var typeShow = ""
        when ( type ){
            "1" ->{
                typeShow = "加热设置"
            }
            "2" ->{
                typeShow = "调味料"
            }
            "3" ->{
                typeShow = "固体料盒"
            }
        }
        return "步骤" + (position + 1) + "：" + typeShow
    }

    //初始化recyclerview
    private fun setRecyclerView(recyclerview: RecyclerView){
        dishesTimePageItemAdapter = DishesTimePageItemAdapter(context, R.layout.adapter_dishes_time_page_item, dishesTimePageItemList)
        recyclerview.apply {
            layoutManager = FlexboxLayoutManager(context).apply {
                flexDirection = FlexDirection.ROW
                flexWrap = FlexWrap.WRAP
                alignItems = AlignItems.STRETCH
            }
            adapter = dishesTimePageItemAdapter
            recyclerview.isNestedScrollingEnabled = false
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    private fun putData(item: DishesCreateBean) {
        dishesTimePageItemBeans = getDishesTimePageItemBeansList(item)

        if ( dishesTimePageItemBeans.isEmpty()) {
            dishesTimePageItemList.clear()
        }
        dishesTimePageItemAdapter!!.notifyDataSetChanged()
    }

    private fun getDishesTimePageItemBeansList(item: DishesCreateBean): MutableList<DishesTimePageItemBean> {
        return try {
            val dishesTimePageItemBeans: MutableList<DishesTimePageItemBean> = mutableListOf()
            val itemList: MutableList<String> = mutableListOf()
            when (item.steep_type){
                "1" ->{
                    itemList.add("时间："+item.time_long+"秒")
                    itemList.add("火力："+item.fire_value)
                    itemList.add("转速："+item.gripper_speed_value)
                    when ( item.gripper_rotation_value ){
                        "0"->{
                            itemList.add("转向：顺时针")
                        }
                        "1"->{
                            itemList.add("转向：逆时针")
                        }
                        "2"->{
                            itemList.add("转向：正反转")
                        }
                    }
                    when ( item.gripper_position_value ){
                        "1"->{
                            itemList.add("搅拌子状态：放下")
                        }
                        "2"->{
                            itemList.add("搅拌子状态：抬起")
                        }
                    }

                }
                "2" ->{
                    itemList.add("时间："+item.time_long+"秒")
                    if ( item.nozzle1_value != "0" ){
                        itemList.add("生抽："+item.nozzle1_value+" g")
                    }
                    if ( item.nozzle2_value != "0" ){
                        itemList.add("老抽："+item.nozzle2_value+" g")
                    }
                    if ( item.nozzle3_value != "0" ){
                        itemList.add("蚝油："+item.nozzle3_value+" g")
                    }
                    if ( item.nozzle4_value != "0" ){
                        itemList.add("动物油："+item.nozzle4_value+" g")
                    }
                    if ( item.nozzle5_value != "0" ){
                        itemList.add("植物油："+item.nozzle5_value+" g")
                    }
                    if ( item.nozzle6_value != "0" ){
                        itemList.add("盐水："+item.nozzle6_value+" g")
                    }
                    if ( item.nozzle7_value != "0" ){
                        itemList.add("醋："+item.nozzle7_value+" g")
                    }
                    if ( item.nozzle8_value != "0" ){
                        itemList.add("淀粉水："+item.nozzle8_value+" g")
                    }
                    if ( item.nozzle9_value != "0" ){
                        itemList.add("烹饪水："+item.nozzle9_value+" g")
                    }
//                    if ( item.nozzle10_value != "0" ){
//                        itemList.add("烹饪水："+item.nozzle10_value+" g")
//                    }
//                    if ( item.nozzle11_value != "0" ){
//                        itemList.add("洗锅水："+item.nozzle11_value+" g")
//                    }
                }
                "3" ->{
                    itemList.add("时间："+item.time_long+"秒")
                    when ( item.box_need_avoid ){
                        "0"->{
                            itemList.add("投菜避让：否")
                        }
                        "1"->{
                            itemList.add("投菜避让：是")
                        }
                    }
                    when ( item.box_release_type ){
                        "1"->{
                            itemList.add("投菜方式：常规投菜（投两下）")
                        }
                        "2"->{
                            itemList.add("投菜方式：慢速投菜（滞留几秒后归位，不抖动）")
                        }
                        "3"->{
                            itemList.add("投菜方式：快速投菜（投三下）")
                        }
                        "4"->{
                            itemList.add("投菜方式：慢速投菜（投三下）")
                        }
                    }
                    if ( item.foodNameList.size > 0 ){
                        for ( i in 0 until item.foodNameList.size){
                            itemList.add(item.foodNameList[i] + "："+ item.foodValueList[i] +" g")
                        }
                    }
                }
            }
            if (itemList.isNotEmpty()) {
                for (i in itemList.indices) {
                    val dishesTimePageItemBean = DishesTimePageItemBean(
                        itemList[i]
                    )
                    dishesTimePageItemBeans.add(dishesTimePageItemBean)
                }
                dishesTimePageItemList.clear()
                dishesTimePageItemList.addAll(dishesTimePageItemBeans)
            }
            dishesTimePageItemBeans
        } catch (e: java.lang.Exception) {
            mutableListOf()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    //刷新数据
    fun updateData(newData: List<DishesCreateBean>) {
        (context as? Activity)?.runOnUiThread {
            (mItems as? MutableList)?.apply {
                clear()
                addAll(newData)
            }
            notifyDataSetChanged()
        }
    }
}